package com.ivi.car.module.timeselection.view.activity;



import android.app.DatePickerDialog;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import com.ivi.car.R;
import com.ivi.car.base.BaseActivity;
import com.ivi.car.common.widget.CustomRadioGroup;
import com.ivi.car.module.timeselection.model.ITimeContract;
import com.ivi.car.module.timeselection.persenter.TimePresenter;
import com.wm.machine.baselibrary.utils.ToastUtils;
import java.util.Calendar;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/7.
 */

public class TimeSelectionActivity extends BaseActivity
        implements CustomRadioGroup.OnCheckedChangeListener, ITimeContract.View {
    @BindView(R.id.im_time_selection_back)
    ImageView imBack;
    @BindView(R.id.tv_time_selection_ok)
    TextView imOk;
    @BindView(R.id.im_time_selection_date_back)
    ImageView imDateback;
    @BindView(R.id.im_time_selection_date_forward)
    ImageView imDateForward;
    @BindView(R.id.tv_time_selection_board)
    TextView tvSelectionBoard;
    @BindView(R.id.rb_time_selection_8)
    RadioButton rbTimeSelection8;
    @BindView(R.id.rb_time_selection_9)
    RadioButton rbTimeSelection9;
    @BindView(R.id.rb_time_selection_10)
    RadioButton rbTimeSelection10;
    @BindView(R.id.rb_time_selection_11)
    RadioButton rbTimeSelection11;
    @BindView(R.id.rb_time_selection_13)
    RadioButton rbTimeSelection13;
    @BindView(R.id.rb_time_selection_14)
    RadioButton rbTimeSelection14;
    @BindView(R.id.rb_time_selection_15)
    RadioButton rbTimeSelection15;
    @BindView(R.id.rb_time_selection_16)
    RadioButton rbTimeSelection16;
    @BindView(R.id.rg_time_selection_group)
    CustomRadioGroup rgGroup;
    private TimePresenter mTimePresenter;
    private final int goToDays = 1;
    @Override
    protected int getLayoutId() {

        return R.layout.activity_time_selection;
    }

    @Override
    protected void initView() {
        rgGroup.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData() {
        mTimePresenter = new TimePresenter(this, mLifecycleProvider);
        String week = mTimePresenter.showWeek();
        if (week != null) {

            tvSelectionBoard.setText(week);
        }
    }
    /**
     * 点击事件处理
     *
     * @param view 点击事件对应的view
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.im_time_selection_back, R.id.im_time_selection_date_back,
            R.id.im_time_selection_date_forward, R.id.tv_time_selection_board})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.im_time_selection_back:

                break;
            case R.id.im_time_selection_date_back:
                String timeBack = mTimePresenter.dateSwitch(-goToDays);
                if (timeBack != null) {
                    tvSelectionBoard.setText(timeBack);
                } else {
                    ToastUtils.showLong(this, "不能点了");
                }
                break;
            case R.id.im_time_selection_date_forward:
                String timeForward = mTimePresenter.dateSwitch(goToDays);
                tvSelectionBoard.setText(timeForward);
                break;
            case R.id.tv_time_selection_board:
                Calendar calendar = Calendar.getInstance();
                DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                        new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

                        boolean isShow = mTimePresenter.dateContrast(i, i1, i2);
                        if (isShow) {
                            int month = i1 + 1;
                            StringBuffer stringBuffer = new StringBuffer();
                            stringBuffer.append(i);
                            stringBuffer.append("-");
                            stringBuffer.append(month);
                            stringBuffer.append("-");
                            stringBuffer.append(i2);
                            String date = stringBuffer.toString();
                            String weekDate = mTimePresenter.dataSplicing(date);
                            tvSelectionBoard.setText(weekDate);
                        } else {
                            ToastUtils.showLong(TimeSelectionActivity.this, "日期选择错误，请重新选择");
                        }
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
                break;

                default:
        }
    }
    @Override
    public void onCheckedChanged(CustomRadioGroup group, int checkedId) {

    }
}
